"""
Case Type   : dsscmd交互式
Case Name   : 交互式模式下，
Create At   : 2024/10
Owner       : haomeng
Description :
    1、进入交互模式，设置配置项
    2、进入交互模式，获取配置项信息
    3、进入交互模式，清理内部锁资源
    4、进入交互模式，一部关闭dssserver进程
Expect      :
    1、设置成功
    2、查看成功
    3、清理成功
    4、关闭dss进程成功，交互式内与dss失去连接
History     :
"""


import os
import unittest

from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class DsscmdInteractive(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.node = Node('PrimaryDbUser')

    def test_dsscmd(self):
        text = '-----step1:进入交互式模式，设置配置项   expect:设置成功-----'
        self.log.info(text)

        cmd = f'''dsscmd -i'''
        execute_cmd1 = f'''source {macro.DB_ENV_PATH};
                     expect <<EOF
                     set timeout 3
                     spawn {cmd}
                     expect "dsscmd>"
                     send "setcfg -n _LOG_LEVEL -v 255 -s both\\n"
                     expect "dsscmd>"
                     send "exit\\n"
                     expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd1)
        res1 = self.node.sh(execute_cmd1).result()
        self.log.info(res1)
        self.assertIn('Succeed to set cfg', res1, '设置配置项失败')

        text = '-----step2:进入交互式模式，获取配置项信息   expect:成功-----'
        self.log.info(text)

        execute_cmd2 = f'''source {macro.DB_ENV_PATH};
                     expect <<EOF
                     set timeout 3
                     spawn {cmd}
                     expect "dsscmd>"
                     send "getcfg -n _LOG_LEVEL\\n"
                     expect "dsscmd>"
                     send "exit\\n"
                     expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd2)
        res2 = self.node.sh(execute_cmd2).result()
        self.log.info(res2)
        self.assertIn('Succeed to get cfg', res2, '获取配置项值失败')

        text = '-----step3:进入交互式模式，清理内部锁资源   expect:清理成功-----'
        self.log.info(text)

        execute_cmd3 = f'''source {macro.DB_ENV_PATH};
                     expect <<EOF
                     set timeout 3
                     spawn {cmd}
                     expect "dsscmd>"
                     send "clean_vglock\\n"
                     expect "dsscmd>"
                     send "exit\\n"
                     expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd3)
        res3 = self.node.sh(execute_cmd3).result()
        self.log.info(res3)
        self.assertIn('Succeed to clean vg lock', res3, '清理内部锁资源失败')

        text = '-----step4:进入交互式模式，异步关闭dssserver进程  expect:关闭dss进程成功，交互式内与dss失去连接-----'
        self.log.info(text)

        execute_cmd4 = f'''source {macro.DB_ENV_PATH};
                     expect <<EOF
                     set timeout 3
                     spawn {cmd}
                     expect "dsscmd>"
                     send "stopdss\\n"
                     sleep 3
                     expect "dsscmd>"
                     send "ls -p +data\\n"
                     expect "dsscmd>"
                     send "exit\\n"
                     expect eof\n''' + '''EOF'''
        self.log.info(execute_cmd4)
        res4 = self.node.sh(execute_cmd4).result()
        self.log.info(res4)
        self.assertIn('Succeed to stop server', res4, '关闭dss进程失败')
        self.assertIn('Failed to get uds connection', res4, '连接未丢失')

    def tearDown(self):
        text = '-----No need to clean-----'
        self.log.info(text)
        self.log.info(f'----{os.path.basename(__file__)} end----')
